const state={
	//要设置的全局访问的state对象
	teacherlist:[],//教室列表
	assistlist:[],//助教列表
	classroomlist:[],//教室列表
	coursePakagelist:[],//最大课程包列表
	coursePakageAlllist:[],//所有课程包
	chinaCitylist:[],
   };

const getters = { 
	
	getTeacherlist(state){
		return state.teacherlist;
	},
	getAssistlist(state){
		return state.assistlist;
	},
	getClassroomlist(state){
		return state.classroomlist;
	},
	getCoursePakagelist(state){
		return state.coursePakagelist;
	},
	getCoursePakageAlllist(state){
		return state.coursePakageAlllist;
	},
	getChinaCitylist(state){
		return state.chinaCitylist;
	}
};

const mutations = {

     setTeacherlist(state,data){
     	state.teacherlist=data;
     },
     setAssistlist(state,data){
     	state.assistlist=data;
     },
     setClassroomlist(state,data){
     	state.classroomlist=data;
     },
     setCoursePakagelist(state,data){
     	state.coursePakagelist=data;
     },
     setCoursePakageAlllist(state,data){
     	state.coursePakageAlllist=data;
     },
     setChinaCitylist(state,data){
     	state.chinaCitylist=data;
     }
};

const actions = {
	
	initTeacherlist(context,para){
		
		api.userListByCampus(para).then(function(data){
			context.commit("setTeacherlist",data.userList)
		})
	},
	initAssistlist(context,para){
		
		api.userListByCampus(para).then(function(data){
			context.commit("setAssistlist",data.userList)
		})
	},
	initClassroomlist(context,para){
		
		api.getClassroomList(para).then(function(data){
			
			context.commit("setClassroomlist",data.info)
			
		})
	},
	initCoursePakagelist(context,para){
		let tempdata=[];
		let tempalldata=[];
		api.courseList(para).then(function(data){
			console.log(data.list);
			//获取课程最大课程包
			tempdata = data.list.map(function(row){				
				if(row.maxHourCoursePackage!=null){	
					return {
				 		name:row.courseName,
				 		value:row.maxHourCoursePackage.coursePackageId,
				 		courseHour:row.maxHourCoursePackage.courseHour,
				 		subjectName:row.subjectName,
				 		courseTypeName:row.courseTypeName,
				 		courseMode:row.courseMode,
				 		courseTypeId:row.courseType,
				 		courseTimeMode:row.maxHourCoursePackage.courseTimeMode, //1.年 2.半年 3.一季度 4.一个月
						 courseId:row.courseId,
						 oneSyllabusDuration:row.libraryCourseSyllabusList.length == 0? "0":row.libraryCourseSyllabusList[0].oneSyllabusDuration,
				 		syllabusNum:row.maxHourCoursePackage.syllabusNum,
				 		allCoursePackage:row.coursePackageEntityList
				 	}
				}else{				
					return null;
				}
			});
		
			//获取所有课程包
//			for(let i=0;i<data.list.length;i++){
//				let item = data.list[i].coursePackageEntityList;
//				for(let j=0;j<item.length;j++){
//					tempalldata.push({
//						name:data.list[i].courseName,
//						value:item[j].coursePackageId,
//						courseHour:item[j].courseHour,
//						subjectName:data.list[i].subjectName,
//						courseTypeName:'',
//						courseMode:data.list[i].courseMode,
//						courseTypeId:data.list[i].courseType,
//						courseTimeMode:item[j].courseTimeMode,
//						courseId:data.list[i].courseId,
//						syllabusNum:item[j].syllabusNum
//					})
//				}			
//			}			
			deleteArr(tempdata);					
			context.commit("setCoursePakagelist",tempdata);
			//context.commit("setCoursePakageAlllist",tempalldata)
		}).catch(function(e){
			console.log(e)
		})
	},
	initChinaCitylist(context){
		let citysData = require('@/../static/citys.json'); 
		context.commit("setChinaCitylist",citysData)
	}

};

function deleteArr(arr){
	//删除为null的元素
	for(let i = 0;i<arr.length;i++){
		
		if(arr[i]==null){
			arr.splice(i,1);
			break;
		}
	}
	for(let i = 0;i<arr.length;i++){
		
		if(arr[i]==null){
			deleteArr(arr);
			break;
		}
	}
	


	
}

module.exports = {
	  namespaced:true,//用于在全局引用此文件里的方法时标识这一个的文件名
	  state,
	  getters,
	  actions,
	  mutations
}
